<template>
  <el-dialog
    :title="!dataForm.id ? '修改' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <div class="mod-user">
    <!-- <el-tabs type="border-card"> -->
      <!-- <el-tab-pane label="修改积分"> -->
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="updateShoppingVoucher()" class="data-form">
          <el-row >
            <el-form-item label="会员账号:">
              <el-input v-model="dataForm.name" placeholder="会员账号" clearable @change="nameChange()" style="width:300px"></el-input>
            </el-form-item>
          </el-row>

          <el-row >
            <el-form-item label="现有佣金:">
              <span>{{dataForm.commissionMoney}}</span>
            </el-form-item>
          </el-row>

          <el-row>
            <el-form-item label="操作类型:">
              <el-radio v-model="dataForm.type" label="0" border>增加</el-radio>
              <el-radio v-model="dataForm.type" label="1" border>减少</el-radio>
            </el-form-item>
          </el-row>

          <el-row>
            <el-form-item label="变更值:">
              <el-input v-model="dataForm.num" style="width: 200px">
                <template slot="prepend">{{dataForm.type === '0' ? '+' : '-'}}</template>
              </el-input>
            </el-form-item>
          </el-row>

          <el-row>
            <el-form-item label="操作说明:">
              <textarea v-model="dataForm.remark" style="width:300px; height:60px"></textarea>
            </el-form-item>
          </el-row>

          <el-row>
            <el-form-item>
              <el-button @click="updateShoppingVoucher()">修改</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      <!-- </el-tab-pane> -->
    <!-- </el-tabs> -->
  </div>
    <!-- <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span> -->
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        dataForm: {
          name: '',
          commissionMoney: 0,
          type: '',
          num: 0,
          remark: ''
        },
        dataRule: {}
      }
    },
    methods: {
      init (userName) {
        this.visible = true
        this.dataForm.num = 0
        this.dataForm.commissionMoney = 0
        this.dataForm.type = ''
        this.dataForm.remark = ''
        this.dataForm.name = ''
        this.dataForm.name = userName
        this.nameChange()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      // 表单提交
      updateShoppingVoucher () {
        if (this.dataForm.name === '') {
          this.$message('账号不能为空')
          return
        }
        if (this.dataForm.type === '') {
          this.$message('类型不能为空')
          return
        }
        if (this.dataForm.remark === '') {
          this.$message('备注不能为空')
          return
        }
        if (this.dataForm.num === 0) {
          this.$message('交易佣金数量不得为0！')
          return
        }
        let reg = /^(0|[1-9]\d*)(\.\d{1,2})?$/
        if (!reg.test(this.dataForm.num)) {
          this.$message('佣金数量不合法！')
          return
        }
        if (this.dataForm.commissionMoney < this.dataForm.num && this.dataForm.type === '1') {
          this.$message('当前修改佣金大于用户现有佣金数量！')
          return
        }
        this.$confirm('确认修改吗？此操作不可撤回。')
          .then(_ => {
            this.$http({
              url: this.$http.adornUrl('/balanceLog/modifyUserCommissionMoney'),
              method: 'post',
              params: this.$http.adornParams({
                'name': this.dataForm.name,
                'num': this.dataForm.num,
                'type': this.dataForm.type,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = ''
                this.dataForm.num = 0
                this.dataForm.remark = ''
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          })
      },
      nameChange () {
        this.$http({
          url: this.$http.adornUrl(`/balanceLog/getCommissionMoneyByUserName/${this.dataForm.name}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.commissionMoney = data.data
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
